import {
    GetOrderDetails,
    SetOrderTag,
    MarkedReceipt,
    SetExtendedDay,
    SetStoreOrderConfig,
    GetStoreOrderConfig,
    ActiveRefund,
    GetRefundMaxFee
} from '@/services/order/orderdetail';
import {catchException} from '@/utils/utils';
export default {
    namespace: 'orderdetail',

    state: {
        detail: {},
        orderConfig:{},
        refundMaxFee:{}
    },

    effects: {

        // 获取订单列表
        *GetOrderDetail({ params, callBack }, { call, put }) {
            const data = yield call(GetOrderDetails, params);
            yield catchException(function* (){
                if (data.Code === 0) {
                    yield put({
                        type: 'OrderDetail',
                        detail: data.Data,
                    });
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
              })             
        },
        *SetOrderTag({ params, callBack }, { call, put }) {
            const data = yield call(SetOrderTag, params);
            yield catchException(function* (){
                if (data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
              })             
        },
        *MarkedReceipt({ params, callBack }, { call, put }) {
            const data = yield call(MarkedReceipt, params);
            yield catchException(function* (){
                if (data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
              })             
        },
        *SetExtendedDay({ params, callBack }, { call, put }) {
            const data = yield call(SetExtendedDay, params);
            yield catchException(function* (){
                if (data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
              })             
        },
        *GetStoreOrderConfig({ params, callBack }, { call, put }) {
            const data = yield call(GetStoreOrderConfig, params);
            yield catchException(function* (){
                if (data.Code === 0) {
                    yield put({
                        type: 'SaveStoreOrderConfig',
                        config: data.Data,
                    });
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
              })            
        },

        //获取可退
        *GetRefundMaxFee({payload, callBack}, {call, put}) {
        const data = yield call(GetRefundMaxFee, payload);
        yield catchException(function* (){
            if(data.Code === 0) {
                yield put({
                    type: 'saveGetRefundMaxFee',
                    payload: {
                        dataList: data.Data,
                    },
                });
                callBack && callBack.success(data);
            } else {
                callBack && callBack.error(data)
            }
          })         
    },

        *SetStoreOrderConfig({ params, callBack }, { call, put }) {
            const data = yield call(SetStoreOrderConfig, params);
            yield catchException(function* (){
                if (data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
              })             
        },

        // 退款
        *ActiveRefund({payload, callBack}, {call, put}) {
            const data = yield call(ActiveRefund, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
              })             
          },
    },

    reducers: {
        OrderDetail(state, { detail }) {
            return {
                ...state,
                detail: detail,
            };
        },
        SaveStoreOrderConfig(state, { config }) {
            return {
                ...state,
                orderConfig: config,
            };
        },
        saveGetRefundMaxFee(state, { config }) {
            return {
                ...state,
                refundMaxFee: config,
            };
        },

    }
}
